﻿using System.Data;
using System.Data.SQLite;
using System.Text;
using CompactMapper.Orm.Contracts;
using CompactMapper.Orm.UnitOfWork.Mappings;

namespace CompactMapper.Orm.UnitOfWork.Commands.Criteria
{
    internal sealed class ForeignKeyEqualsCriterion<T> : Criterion<T>
    {
        private const string Equal = " = ";


        public ForeignKeyEqualsCriterion(Queries.Criterion<T> criterion, TablesTree tablesTreeRoot)
            : base(criterion, tablesTreeRoot)
        {
        }

        public override SQLiteParameter SQLiteParameter()
        {
            var parameter = new SQLiteParameter
                                {
                                    ParameterName = "@" + PropertyName,
                                    Value = ((IKey) TargetValue).Value
                                };
            return parameter;
        }

        protected override string ToSql()
        {
            var criterionTextBuilder = new StringBuilder();
            criterionTextBuilder.Append(PropertyName);
            criterionTextBuilder.Append(Equal);
            criterionTextBuilder.Append("@").Append(PropertyName);
            return criterionTextBuilder.ToString();
        }
    }
}